package com.innovations.inn.service.impl;

import com.innovations.common.utils.DateUtils;
import static com.innovations.common.utils.MyBeanUtils.copyProperties;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.BusinessIndividual;
import com.innovations.inn.domain.BusinessIndividualModify;
import com.innovations.inn.domain.response.ResponseBusinessIndividual;
import com.innovations.inn.domain.response.ResponseItemCount;
import com.innovations.inn.mapper.BusinessIndividualModifyMapper;
import com.innovations.inn.service.IBusinessIndividualModifyService;
import com.innovations.inn.service.IBusinessIndividualService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商业用户信息修改记录Service业务层处理
 *
 * @author dp
 * @date 2024-11-08
 */
@Service
public class BusinessIndividualModifyServiceImpl implements IBusinessIndividualModifyService {
    @Autowired
    BusinessIndividualModifyMapper businessIndividualModifyMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    IBusinessIndividualService businessIndividualService;

    /**
     * 查询商业用户信息修改记录
     *
     * @param businessIndividualId 商业用户信息修改记录主键
     * @return 商业用户信息修改记录
     */
    @Override
    public BusinessIndividualModify selectBusinessIndividualModifyByBusinessIndividualId(Long businessIndividualId) {
        return businessIndividualModifyMapper.selectBusinessIndividualModifyByBusinessIndividualId(businessIndividualId);
    }

    /**
     * 查询商业用户信息修改记录列表
     *
     * @param businessIndividualModify 商业用户信息修改记录
     * @return 商业用户信息修改记录
     */
    @Override
    public List<BusinessIndividualModify> selectBusinessIndividualModifyList(BusinessIndividualModify businessIndividualModify) {
        return businessIndividualModifyMapper.selectBusinessIndividualModifyList(businessIndividualModify);
    }

    /**
     * 查询商业用户扩展信息修改记录列表
     *
     * @param businessIndividualModify 商业用户信息修改记录
     * @return 商业用户信息修改记录集合
     */
    @Override
    public List<ResponseBusinessIndividual> selectBusinessIndividualModifyListInfo(BusinessIndividualModify businessIndividualModify) {
        return businessIndividualModifyMapper.selectBusinessIndividualModifyListInfo(businessIndividualModify);
    }


    /**
     * 查询商业用户扩展信息修改记录合计
     *
     * @param businessIndividualModify 商业用户信息修改记录
     * @return 商业用户信息修改记录合计
     */
    @Override
    public List<ResponseItemCount> selectBusinessIndividualModifyListInfoCount(BusinessIndividualModify businessIndividualModify) {
        return businessIndividualModifyMapper.selectBusinessIndividualModifyListInfoCount(businessIndividualModify);
    }


    /**
     * 检查商业用户信息修改记录是否唯一
     *
     * @param businessIndividualModify 商业用户信息修改记录
     * @return 结果
     */
    @Override
    public boolean checkBusinessIndividualModifyUnique(BusinessIndividualModify businessIndividualModify) {
        //如果当前为提交审核状态，则校验是否存在未审核的记录
        if (businessIndividualModify.getState() == 1) {
            BusinessIndividualModify businessIndividualModifyQuery = new BusinessIndividualModify();
            businessIndividualModifyQuery.setBusinessIndividualId(businessIndividualModify.getBusinessIndividualId());
            businessIndividualModifyQuery.setState(1L);
            List<BusinessIndividualModify> businessIndividualModifyList = businessIndividualModifyMapper
                    .selectBusinessIndividualModifyList(businessIndividualModifyQuery);
            return businessIndividualModifyList == null || businessIndividualModifyList.isEmpty();
        }
        return true;
    }

    /**
     * 新增商业用户信息修改记录
     *
     * @param businessIndividualModify 商业用户信息修改记录
     * @return 结果
     */
    @Override
    public int insertBusinessIndividualModify(BusinessIndividualModify businessIndividualModify) {
        return businessIndividualModifyMapper.insertBusinessIndividualModify(businessIndividualModify);
    }

    /**
     * 修改商业用户信息修改记录
     *
     * @param businessIndividualModify 商业用户信息修改记录
     * @return 结果
     */
    @Override
    public int updateBusinessIndividualModify(BusinessIndividualModify businessIndividualModify) {
        // 状态为审核通过时，添加商业角色
        if (businessIndividualModify.getState() == 2) {
            // 获取商业用户信息
            BusinessIndividual businessIndividual = businessIndividualService
                    .selectBusinessIndividualByBusinessIndividualId(businessIndividualModify.getBusinessIndividualId());
            // 新增或修改商业用户信息
            BusinessIndividual businessIndividualNew = copyProperties(businessIndividualModify, BusinessIndividual.class);
            if (businessIndividual == null || businessIndividual.getBusinessIndividualId() == null) {
                businessIndividualService.insertBusinessIndividual(businessIndividualNew);
            } else {
                businessIndividualService.updateBusinessIndividual(businessIndividualNew);
            }

        }
        // 更新商务用户信息修改记录
        return businessIndividualModifyMapper.updateBusinessIndividualModify(businessIndividualModify);
    }

    /**
     * 批量删除商业用户信息修改记录
     *
     * @param businessIndividualIds 需要删除的商业用户信息修改记录主键
     * @return 结果
     */
    @Override
    public int deleteBusinessIndividualModifyByBusinessIndividualIds(Long[] businessIndividualIds) {
        return businessIndividualModifyMapper.deleteBusinessIndividualModifyByBusinessIndividualIds(businessIndividualIds);
    }

    /**
     * 删除商业用户信息修改记录信息
     *
     * @param businessIndividualId 商业用户信息修改记录主键
     * @return 结果
     */
    @Override
    public int deleteBusinessIndividualModifyByBusinessIndividualId(Long businessIndividualId) {
        return businessIndividualModifyMapper.deleteBusinessIndividualModifyByBusinessIndividualId(businessIndividualId);
    }

    /**
     * 实体数据补全
     *
     * @param businessIndividualModify 实体
     * @param userId                   用户id
     * @param crateType                创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(BusinessIndividualModify businessIndividualModify, Long userId, Integer crateType) {
        // 标识
        if (businessIndividualModify.getModifyId() == null) {
            businessIndividualModify.setModifyId(idGeneratorSnowflake.nextId());
        }
        // 商业用户标识
        if (businessIndividualModify.getBusinessIndividualId() == null) {
            // 根据用户标识获取商业用户标识
            BusinessIndividual businessIndividual = new BusinessIndividual();
            businessIndividual.setUserId(userId);
            List<BusinessIndividual> enterpriseList = businessIndividualService.selectBusinessIndividualList(businessIndividual);
            // 判断企业列表为空，则创建新企业标识
            if (enterpriseList == null || enterpriseList.isEmpty()) {
                businessIndividualModify.setBusinessIndividualId(idGeneratorSnowflake.nextId());
            } else {
                businessIndividualModify.setBusinessIndividualId(enterpriseList.get(0).getBusinessIndividualId());
            }
        }
        // 创建模式
        if (crateType == 1) {
            // 创建人、创建时间
            if (businessIndividualModify.getCreateBy() == null || "".equals(businessIndividualModify.getCreateBy())) {
                businessIndividualModify.setCreateBy(userId);
            }
            if (businessIndividualModify.getCreateTime() == null) {
                businessIndividualModify.setCreateTime(DateUtils.getNowDate());
            }
        }
        // 修改模式
        else if (crateType == 2) {

        }
        // 逻辑删除
        if (businessIndividualModify.getIsDeleted() == null || "".equals(businessIndividualModify.getIsDeleted())) {
            businessIndividualModify.setIsDeleted(0L);
        }
    }

}
